'use strict';

module.exports = function createUpdatedCollection(collectionA, objectB) {
  let arr = [];
  let collection = [].concat(makeCollection(collectionA))
  for(let i of collection){
    let a = Object.keys(i);
    if(minerstheSame(objectB,i.key)){
        i.count=i.count-parseInt(i.count/3);
    }
    arr.push({
      key: i.key,
      count: i.count
    })
  }
 
  return arr; 
}

function minerstheSame(obj,i){
  for (let a in obj){
    for(let item of obj[a]){
      if(item == i){
        return true;
      }
    }
  }
  return false;
}

function makeCollection(a){
  let arr = [];
  for(let i = 0; i < a.length;){
    let count = 0;
    for(let j = i; j < a.length; j++){
      if (a[i] === a[j]) {
        count++;
      }
    }
    arr.push({
      key: a[i],
      count: count
    })
    i+=count;
  }
  return arr;
}